//
//  ApplicationModel.m
//  WSPluginSwitcher
//
//  @author	Jens Krause [www.websector.de]
//	@date		01/01/09
//  @see		http://www.websector.de/blog/
//
//  Licensed under the MIT License http://www.opensource.org/licenses/mit-license.php
//


#import "ApplicationModel.h"

@interface ApplicationModel (Private)
- (FlashPlugInVO *)_plugInAtPath:(NSString *)path;
@end


@implementation ApplicationModel

NSString *const KEY_ARCHIVE_FLASH_PLUGINS_FOLDERS = @"keyArchivFlashPluginsFolder";
NSString *const KEY_RESTART_FIREFOX = @"keyRestartFirefox";
NSString *const KEY_RESTART_SAFARI = @"keyRestartSafari";

NSString *const DEFAULT_PLUGINS_FOLDER = @"/Library/Internet Plug-Ins/";

NSString *const KEY_CHECK_FOR_UPDATES = @"SUEnableAutomaticChecks";

@synthesize archiveFlashPluginFolders, installedPlugIn;


#pragma mark -
#pragma mark Initialization & Deallocation

- (id)init
{
	if (self = [super init]) 
	{	
		archiveFlashPluginFolders = nil;
		self.installedPlugIn = [self _plugInAtPath:[DEFAULT_PLUGINS_FOLDER 
			stringByAppendingPathComponent:FILENAME_FLASH_PLAYER_PLUGIN]];
	}
	return self;
}

- (void)dealloc
{	
	[archiveFlashPluginFolders release];
	[installedPlugIn release];
	[super dealloc];
}



#pragma mark -
#pragma mark Public methods

- (void)updateArchiveFlashPluginFolders
{
	NSString *archiveFlashPluginsFolders = [[NSUserDefaults standardUserDefaults] 
		stringForKey:KEY_ARCHIVE_FLASH_PLUGINS_FOLDERS];
	
	if (archiveFlashPluginsFolders == nil)
	{
		return;
	}
	
	NSFileManager *fm = [NSFileManager defaultManager];
	NSArray *dirContents = [fm directoryContentsAtPath:archiveFlashPluginsFolders];
	NSMutableArray *newArchiveFolders = [[NSMutableArray alloc] init];
	for (NSString *filePath in dirContents)
	{
		NSString *path = [archiveFlashPluginsFolders stringByAppendingPathComponent:filePath];
		NSString *xptPath = [path stringByAppendingPathComponent:FILENAME_FLASH_PLAYER_XPT];
		NSString *pluginPath = [path stringByAppendingPathComponent:FILENAME_FLASH_PLAYER_PLUGIN];
		NSURL *pathURL = [NSURL fileURLWithPath:[path 
			stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
		LSItemInfoRecord infoRec;
		OSStatus status = noErr;
		// ignore invisibles and non-directories, just as directories with missing plugin files
		status = LSCopyItemInfoForURL((CFURLRef)pathURL, kLSRequestBasicFlagsOnly, &infoRec);
		if (!status && ((infoRec.flags & kLSItemInfoIsInvisible) || 
			!(infoRec.flags & kLSItemInfoIsContainer) || ![fm fileExistsAtPath:xptPath] || 
			![fm fileExistsAtPath:pluginPath]))
		{
			continue;
		}
		FlashPlugInVO *plugIn = [self _plugInAtPath:pluginPath];
		if (plugIn)
		{
			[newArchiveFolders addObject:plugIn];
		}
	}
	
	NSArray *nonMutableArchiveFolders = [newArchiveFolders copy];
	self.archiveFlashPluginFolders = nonMutableArchiveFolders;
	[nonMutableArchiveFolders release];
	[newArchiveFolders release];
}

- (BOOL)containsPlugIn:(FlashPlugInVO *)aPlugIn
{
	for (FlashPlugInVO *plugIn in archiveFlashPluginFolders)
	{
		if ([plugIn.versionString isEqualToString:aPlugIn.versionString])
		{
			return YES;
		}
	}
	return NO;
}

- (void)addPlugIn:(FlashPlugInVO *)aPlugIn
{
	// this happens so rarely that we don't need to keep a NSMutableArray
	self.archiveFlashPluginFolders = [self.archiveFlashPluginFolders arrayByAddingObject:aPlugIn];
}



#pragma mark -
#pragma mark Private methods

- (FlashPlugInVO *)_plugInAtPath:(NSString *)path
{
	// open plugin's info.plist to read version#
	NSDictionary *infoDict = (NSDictionary *)CFBundleCopyInfoDictionaryInDirectory(
		(CFURLRef)[NSURL fileURLWithPath:path]);
	NSString *versionStr = [infoDict objectForKey:@"CFBundleShortVersionString"];
	FlashPlugInVO *plugIn = nil;
	if (versionStr)
	{
		plugIn = [FlashPlugInVO pluginWithPath:[path stringByDeletingLastPathComponent] 
			versionString:versionStr];
	}
	CFRelease(infoDict);
	return plugIn;
}
	
@end